
#ifndef __KERNEL__
#  define __KERNEL__
#endif
#ifndef MODULE
#  define MODULE
#endif
#include <linux/config.h>
#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/tcp.h>
#include <linux/ip.h>
#include <net/ip.h>
#include <linux/proc_fs.h>

#define u16toValue(x)\ 
			((__u16) \
			((((__u16)(x) & (__u16)0x00ffU) << 8) | \
			(((__u16)(x) & (__u16)0xff00U) >> 8) ))
#define u32toValue(x) \
			((__u32)( \
			(((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
			(((__u32)(x) & (__u32)0x0000ff00UL) <<  8) | \
			(((__u32)(x) & (__u32)0x00ff0000UL) >>  8) | \
			(((__u32)(x) & (__u32)0xff000000UL) >> 24) ))

static IpAddressToString(unsigned int i,char *s)
{
    unsigned int j1,j2,j3,j4;
    j1=i&0x000000FF;
    j2=(i&0x0000FF00)>>8;
    j3=(i&0x00FF0000)>>16;
    j4=(i&0xFF000000)>>24;
    sprintf(s,"%u.%u.%u.%u",j4,j3,j2,j1);
}

extern int (*eeFunction02)(struct sk_buff *);

static struct proc_dir_entry *proc_mtd;
static char ProcBuffer[50000];
static char Temp1[1000],Temp2[1000];		
static int WritingLength;

static int procfile_read(char *buffer,char **buffer_location,off_t offset,int buffer_length,int zero)
{
    int len,ReturnLength;
    if(offset >0) return 0;

    *buffer_location = ProcBuffer;
    ReturnLength=WritingLength;
    WritingLength=0;
    return ReturnLength;
}

int PacketInformation(struct sk_buff *skb)
{
    struct iphdr *iph;
    struct tcphdr *th;
    char SourceIP[50],TargetIP[50];
    static int PacketNumber=0;

    
    if(WritingLength+strlen(Temp1) < 50000)
    {      

    	iph = skb->nh.iph;
    
    	Temp1[0]=0;
    	sprintf(Temp2,"--------------------------------- PacketNumber=%d\x0D\x0A",PacketNumber++);
    	strcat(Temp1,Temp2);
    	sprintf(Temp2,"IP:tos=0x%X\x0D\x0A",skb->nh.iph->tos);
    	strcat(Temp1,Temp2);
    	sprintf(Temp2,"IP:tot_len=%d\x0D\x0A",u16toValue(skb->nh.iph->tot_len));
    	strcat(Temp1,Temp2);
    	sprintf(Temp2,"IP:id=%d\x0D\x0A",u16toValue(skb->nh.iph->id));
    	strcat(Temp1,Temp2);
    	sprintf(Temp2,"IP:frag_off=%d\x0D\x0A",u16toValue(skb->nh.iph->frag_off));
    	strcat(Temp1,Temp2);
    	sprintf(Temp2,"IP:ttl=%d\x0D\x0A",skb->nh.iph->ttl);
    	strcat(Temp1,Temp2);
    	sprintf(Temp2,"ICP:protocol=0x%X\x0D\x0A",skb->nh.iph->protocol);
    	strcat(Temp1,Temp2);    	    	    	    	    	    	
    	sprintf(Temp2,"IP:check=0x%04X\x0D\x0A",skb->nh.iph->check);
    	strcat(Temp1,Temp2);    
    	
    	IpAddressToString(u32toValue(skb->nh.iph->saddr),SourceIP);
    	IpAddressToString(u32toValue(skb->nh.iph->daddr),TargetIP);    	
    	sprintf(Temp2,"IP:saddr=%s\x0D\x0A",SourceIP);
    	strcat(Temp1,Temp2);    
    	sprintf(Temp2,"IP:daddr=%s\x0D\x0A",TargetIP);
    	strcat(Temp1,Temp2);  
    
    	if(iph->protocol == IPPROTO_TCP) 
    	{
	        th = skb->h.th;      
    		sprintf(Temp2,"TCP:Source Port=%u\x0D\x0A",u16toValue(th->source));  
    		strcat(Temp1,Temp2);      		
    		sprintf(Temp2,"TCP:Destination Port=%u\x0D\x0A",u16toValue(th->dest));
    		strcat(Temp1,Temp2);    
    		sprintf(Temp2,"TCP:seq=%d\x0D\x0A",u32toValue(th->seq));
    		strcat(Temp1,Temp2);    
    		sprintf(Temp2,"TCP:ack_seq=%d\x0D\x0A",u32toValue(th->ack_seq));
    		strcat(Temp1,Temp2);    
    		sprintf(Temp2,"TCP:window=0x%04X\x0D\x0A",u16toValue(th->window));
    		strcat(Temp1,Temp2);    
    		sprintf(Temp2,"TCP:check=0x%04X\x0D\x0A",u16toValue(th->check)); 
    		strcat(Temp1,Temp2);        		    		    		    		    		
    		sprintf(Temp2,"TCP:urg_ptr=0x%04X\x0D\x0A",u16toValue(th->urg_ptr));
    		strcat(Temp1,Temp2);     		
    	}    
        memcpy(&ProcBuffer[WritingLength],Temp1,strlen(Temp1));
        WritingLength+=strlen(Temp1);    	   	  	    	       	    	    	        
    }  
    
    return 0;
}
int init_module(void)
{
	WritingLength=0;
	if ((proc_mtd = create_proc_entry("eee-dev", 0, 0 )))proc_mtd->read_proc = procfile_read;
    eeFunction02  = PacketInformation;	
	printk("\neeFunction2: PacketInformation install ok!\n");	
	return 0;
}
void cleanup_module(void)
{
	if (proc_mtd)remove_proc_entry( "eee-dev", 0);
    eeFunction02 = 0;
    printk("\neeFunction2: PacketInformation uninstall ok!\n");
}
MODULE_LICENSE("GPL");


